/**
 * 
 */
package com.wgo.precise.client.common.session;

import java.rmi.RemoteException;
import java.util.HashSet;

import com.wgo.precise.client.session.Session;
import com.wgo.precise.common.VO.Persistent;
import com.wgo.precise.common.service.SaveRequest;

/**
 * @author peide3
 *
 */
public class PreciseSession extends Session {

	public class ObjectHolder<T> {
		private T object;
		public T getObject() {
			return object;
		}
		protected void setObject(T object) {
			this.object = object;
		}
	}
	
	//TODO: removed functionality:
	public void getPdf(Persistent persistent, ObjectHolder<byte[]> objectHolder ){}

	//TODO: removed functionality:
	public void importFromXml(byte[] preciseXmlFile, ObjectHolder<Boolean> objectHolder){}
	
	//TODO: removed functionality:
	public void exportToXml(ObjectHolder<byte[]> objectHolder){
		try {
			objectHolder.setObject(super.exportToXml());
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}

	public boolean isDirty(Persistent persistent) {
		if (newSet.contains(persistent)){
			return true;
		}
		if (newMap.containsValue(persistent)){
			return true;
		}
		for (SaveRequest saveRequest : super.saveSet){
			if (saveRequest.getObj().getDbId() == persistent.getDbId()){
				return true;
			}
		}
		return false;
	}

	public void save() {
		for (SaveRequest saveRequest : saveSet){
			saveRequest.getObj().setSession(null, new HashSet<Persistent>());
		}
		super.saveAll();
	}

	public boolean isAnyDirty() {
		return ! saveSet.isEmpty() || ! newSet.isEmpty() || ! newMap.isEmpty();
	}

	public String getServerContextUrl() {
		// TODO Auto-generated method stub
		return null;
	}

}
